package com.lab.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lab.dao.MajorDao;
import com.lab.dao.impl.MajorDaoImpl;
import com.lab.po.PageBean;

/**
 * Servlet implementation class MajAjax
 */
@WebServlet("/majPageAjax")
public class MajPageAjax extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MajPageAjax() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			int currentPage=1;
			if(request.getParameter("currentPage")!=null){
				currentPage=Integer.parseInt(request.getParameter("currentPage"));
			}
			int col_id = Integer.parseInt(request.getParameter("col_id"));
			//ajax请求
			MajorDao majordao=new MajorDaoImpl();
			PageBean pagebean=new PageBean();
			pagebean.setCurrentPage(currentPage);

			pagebean.setCountRecords(majordao.countMajorByCol_id(col_id));
			if(currentPage==1){
				pagebean.setPageUp(1);
			}else{
				pagebean.setPageUp(currentPage-1);
			}
			if(currentPage==pagebean.getTotalPage()){
				pagebean.setPageDown(pagebean.getTotalPage());
			}else{
				pagebean.setPageDown(currentPage+1);
			}
			String pb="<tr><td colspan=\"8\">当前页："+pagebean.getCurrentPage()+"/"+pagebean.getTotalPage()
		 			 +"&nbsp;&nbsp;<a  onclick=\"query(1)\">首页</a>&nbsp;&nbsp;<a  onclick=\"query("+pagebean.getPageUp()+")\">上一页</a>&nbsp;&nbsp;<a  onclick=\"query("+pagebean.getPageDown()+")\">下一页</a>"
		 			 +"&nbsp;&nbsp;<a  onclick=\"query("+pagebean.getTotalPage()+")\">末页</a></td></tr>";
			System.out.println(pb);
			response.getWriter().write(pb);
	}

}
